package com.jintian.smart.kernel.flowable.controller;

import com.jintian.smart.kernel.core.flow.ITaskOperation;
import com.jintian.smart.kernel.core.flow.ProcessControl;
import com.jintian.smart.kernel.core.web.RestResultWrapper;
import com.jintian.smart.kernel.flowable.common.utils.BeanUtils;
import com.jintian.smart.kernel.flowable.domain.entity.ActHiTaskInst;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.dubbo.common.utils.Assert;
import org.flowable.engine.ProcessEngine;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程任务相关
 */
@RestResultWrapper
@RestController
@RequestMapping("/flow/singleTask")
@Schema(name = "Task", description = "流程任务2")
@Transactional
public class SingleTaskController {
    @Autowired
    private ITaskOperation<List<TaskInfo>> taskOperation;

    @Autowired
    private ProcessEngine processEngine;

    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "任务完成")
    @Parameters(value = {@Parameter(name = "businessKey", in = ParameterIn.DEFAULT, description = "业务对象ID"),
            @Parameter(name = "processControl", in = ParameterIn.DEFAULT, description = "流程控制")})
    public Object complete(String businessKey, @Nullable @RequestBody ProcessControl processControl) throws Exception {
        List<TaskInfo> ret = taskOperation.complete(this.getBusinessSingleTask(businessKey).getId(), processControl);
        return BeanUtils.copyList(ret, ActHiTaskInst.class);
    }

    private Task getBusinessSingleTask(String businessKey) {
        List<Task> tasks = processEngine.getTaskService().createTaskQuery().processInstanceBusinessKey(businessKey).list();
        Assert.assertTrue(tasks.size() == 1, "存在多个任务实例");
        return tasks.get(0);
    }

    @RequestMapping(value = "/claim", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "任务签收")
    @Parameters(value = {@Parameter(name = "businessKey", in = ParameterIn.DEFAULT, description = "业务对象ID"),
            @Parameter(name = "processControl", in = ParameterIn.DEFAULT, description = "流程控制")})
    public Object claim(String businessKey, @Nullable @RequestBody ProcessControl processControl) throws Exception {
        taskOperation.claim(this.getBusinessSingleTask(businessKey).getId(), processControl);
        return "执行成功";
    }

    @RequestMapping(value = "/unclaim", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "撤销签收")
    @Parameters(value = {@Parameter(name = "businessKey", in = ParameterIn.DEFAULT, description = "业务对象ID"),
            @Parameter(name = "processControl", in = ParameterIn.DEFAULT, description = "流程控制")})
    public Object unclaim(String businessKey, @Nullable @RequestBody ProcessControl processControl) throws Exception {
        taskOperation.unclaim(this.getBusinessSingleTask(businessKey).getId(), processControl);
        return "执行成功";
    }

    @RequestMapping(value = "/transform", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "转发")
    @Parameters(value = {@Parameter(name = "businessKey", in = ParameterIn.DEFAULT, description = "业务对象ID"),
            @Parameter(name = "processControl", in = ParameterIn.DEFAULT, description = "流程控制")})
    public Object transform(String businessKey, @Nullable @RequestBody ProcessControl processControl) throws Exception {
        List<TaskInfo> ret = taskOperation.transform(this.getBusinessSingleTask(businessKey).getId(), processControl);
        return BeanUtils.copyList(ret, ActHiTaskInst.class);
    }

    @RequestMapping(value = "/fallback", method = RequestMethod.POST)
    @Operation(summary = "任务回退")
    @Parameters(value = {@Parameter(name = "businessKey", in = ParameterIn.DEFAULT, description = "业务对象ID"),
            @Parameter(name = "processControl", in = ParameterIn.DEFAULT, description = "流程控制")})
    public Object fallback(String businessKey, @Nullable @RequestBody ProcessControl processControl) throws Exception {
        List<TaskInfo> ret = taskOperation.fallback(this.getBusinessSingleTask(businessKey).getId(), processControl);
        return BeanUtils.copyList(ret, ActHiTaskInst.class);
    }

    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    @Operation(summary = "任务取回")
    @Parameters(value = {@Parameter(name = "businessKey", in = ParameterIn.DEFAULT, description = "业务对象ID"),
            @Parameter(name = "processControl", in = ParameterIn.DEFAULT, description = "流程控制")})
    public Object withdraw(String businessKey, @Nullable @RequestBody ProcessControl processControl) throws Exception {
        taskOperation.withdraw(this.getBusinessSingleTask(businessKey).getId(), processControl);
        return "执行成功";
    }
}
